package com.yehuishou.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.yehuishou.pojo.BackItem;
import com.yehuishou.pojo.Brand;
import com.yehuishou.service.BackItemService;
import com.yehuishou.service.BrandService;
/**
 * 
 * @author zxb
 *
 */

@Controller
public class ClassifyController {

	@Autowired
	private BrandService brandService;
	@Autowired
	private BackItemService backItemService;
	
	@ResponseBody
	@RequestMapping("findBycid")
	public List<Brand> findBycid(Integer cid , Model model) {
		Map<Object , List<BackItem>> map = new HashMap<>();
		List<Brand> list = brandService.findBycid(cid);
		for (Brand brand : list) {
			List<BackItem> li = backItemService.findBypid(brand.getPid());
			brand.setList(li);
		}
		return list;
	}
	
	@RequestMapping("shouji")
	//@ResponseStatus(value=HttpStatus.OK)
	public String findShouJi(Integer cid,Integer pid ,Model model) {
		//通过pid查询所有型号
		if(pid!=null) {
			List<BackItem> list = backItemService.findBypid(pid);
			model.addAttribute("list", list);
			return"";
		}
		//查询cid下的所有品牌
		List<Brand> list = brandService.findBycid(cid);
		model.addAttribute("list", list);
		return"";
	}
	
	//查品牌
	@RequestMapping("fbrand")
	public String findP(Integer cid ,Model model) {
		List<Brand> list = brandService.getBycidAll(cid);
		List<BackItem> item = backItemService.findBycid(cid);
		model.addAttribute("list", list);
		model.addAttribute("item", item);
		model.addAttribute("cid", cid);
		return"shouji";
	}
	
}
